#coding:utf-8
import time,os,logging
from str2arry import str2arry
from serialCom import serialCom
from checkSum import hex_checksum
from time import strftime

FILE = os.getcwd()          #返回当前目录的路径
#logging基础配置，文件名为 os.path.join里面的两个值想加个，输出等级为≥DEBUG级别
logging.basicConfig(filename = os.path.join(FILE,'log.txt'),level = logging.DEBUG,
                    format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s')

def setTAndP():
    print '----------------------------------------------------------'
    print 'case7 设置温度和压力补偿' + '\n' + '                                    '


    # 读取表号
    meterIDread = (
        ''.join(('FE FE FE FE FE 68 AB 57 58 AA AA AA AA AA F2 77 88 99 55 03 00 03 81 0A 01 85 16').split()))
    ID = serialCom(meterIDread, 35)[28:38]
    ID = str2arry(ID)                                #返回值为数组
    logging.debug('获取表号 %s 用于设置参数',''.join(ID))
    # 读取表号
    logging.debug('*****case7 设置温度和压力补偿*****')

    #不带温度补偿命令
    TemperatureOffData='FE FE FE FE FE 68 AB 57 58 '.split() + ID+ 'F2 77 88 99 55 01 00 06 26 00 01 00 00 00 '.split()
    TemperatureOffCheckSum = hex_checksum(TemperatureOffData[5:])
    frame_TemperatureOff=''.join(TemperatureOffData+TemperatureOffCheckSum)+'16'
    #带温度补偿命令
    TemperatureOnData='FE FE FE FE FE 68 AB 57 58 '.split() + ID+ 'F2 77 88 99 55 01 00 06 26 00 01 00 00 01 '.split()
    TemperatureOnCheckSum = hex_checksum(TemperatureOnData[5:])
    frame_TemperatureOn=''.join(TemperatureOnData+TemperatureOnCheckSum)+'16'
    #不带压力补偿命令
    PresureOffData='FE FE FE FE FE 68 AB 57 58 '.split() + ID+ 'F9 77 88 99 55 01 00 06 50 00 01 00 00 00 '.split()
    PresureOffCheckSum = hex_checksum(PresureOffData[5:])
    frame_PresureOff=''.join(PresureOffData+PresureOffCheckSum)+'16'
    #带压力补偿命令
    PresureOnData='FE FE FE FE FE 68 AB 57 58 '.split() + ID+ 'F9 77 88 99 55 01 00 06 50 00 01 00 00 01 '.split()
    PresureOnCheckSum = hex_checksum(PresureOnData[5:])
    frame_PresureOn=''.join(PresureOnData+PresureOnCheckSum)+'16'
    #读取是否带温度补偿
    T_ReadData='FE FE FE FE FE 68 AB 57 58'.split() +ID+  'F2 77 88 99 55 01 00 03 27 00 01'.split()
    T_ReadCheckSum =hex_checksum(T_ReadData[5:])
    frame_ReadT=''.join(T_ReadData+T_ReadCheckSum)+'16'
    #读取是否带压力补偿
    P_ReadData='FE FE FE FE FE 68 AB 57 58'.split() +ID+  'F9 77 88 99 55 01 00 03 51 00 01'.split()
    P_ReadCheckSum =hex_checksum(P_ReadData[5:])
    frame_ReadP=''.join(P_ReadData+P_ReadCheckSum)+'16'

#取消温度和压力补偿
    serialCom(frame_TemperatureOff)
    logging.debug('关温补%s'%frame_TemperatureOff)

    serialCom(frame_PresureOff)
    logging.debug('关压补%s' % frame_PresureOff)

    T_Receive=serialCom(frame_ReadT,36)

    P_Receive=serialCom(frame_ReadP,36)

    # print '读温补%s'%T_Receive  ,T_Receive[54:56]
    # print '读压补%s'%P_Receive  ,P_Receive[54:56]

    if T_Receive[54:56]=='00' and P_Receive[54:56]=='00':
        logging.debug("不带温度，不带压力补偿设置成功")

    else:
        logging.debug("不带温度，不带压力补偿设置失败")




#带温度不带压力补偿
    serialCom(frame_TemperatureOn)
    logging.debug('开温补%s' % frame_TemperatureOn)

    serialCom(frame_PresureOff)
    # print '关压力补%s' % frame_PresureOff
    logging.debug('关压力补%s' % frame_PresureOff)

    T_Receive=serialCom(frame_ReadT,36)

    P_Receive=serialCom(frame_ReadP,36)

    # print '读取是否带温补%s'%T_Receive
    # print '读取是否带压力补%s'%P_Receive
    # print T_Receive[54:56]
    # print P_Receive[54:56]
    if T_Receive[54:56]=='01' and P_Receive[54:56]=='00':
        # print "带温度，不带压力补偿设置成功"
        logging.debug("带温度，不带压力补偿设置成功")
    else:
        # print "带温度，不带压力补偿设置失败"
        logging.debug("带温度，不带压力补偿设置失败")
#不带温度带压力补偿
    serialCom(frame_TemperatureOff)
    # print '关温补%s' % frame_TemperatureOff
    logging.debug('关温补%s' % frame_TemperatureOff)

    serialCom(frame_PresureOn)
    # print '开压补%s' % frame_PresureOn
    logging.debug('开压补%s' % frame_PresureOn)

    T_Receive=serialCom(frame_ReadT,36)

    P_Receive=serialCom(frame_ReadP,36)

    # print T_Receive
    # print P_Receive
    # print T_Receive[54:56]
    # print P_Receive[54:56]
    if T_Receive[54:56]=='00' and P_Receive[54:56]=='01':
        # print "不带温度，带压力补偿设置成功"
        logging.debug("不带温度，带压力补偿设置成功")
    else:
        # print "不带温度，带压力补偿设置失败"
        logging.debug("不带温度，带压力补偿设置失败")
#带温度和压力补偿
    serialCom(frame_TemperatureOn)
    # print '开温补%s' % frame_TemperatureOn
    logging.debug('开温补%s' % frame_TemperatureOn)

    serialCom(frame_PresureOn)
    # print '开压补%s' % frame_PresureOn
    logging.debug('开压补%s' % frame_PresureOn)

    T_Receive=serialCom(frame_ReadT,36)

    P_Receive=serialCom(frame_ReadP,36)

    # print T_Receive
    # print P_Receive
    # print T_Receive[54:56]
    # print P_Receive[54:56]
    if T_Receive[54:56]=='01' and P_Receive[54:56]=='01':
        # print "带温度，带压力补偿设置成功"
        logging.debug("带温度，带压力补偿设置成功")
    else:
        print "带温度，带压力补偿设置失败"
        logging.debug("带温度，带压力补偿设置失败")

